package persentation.presentTeacher;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;

import dataservice.ResultMessage;
import businesslogic.courseBl.CourseController;
import vo.courseVO.CourseVO;

public class FillCourseInfoPanel extends JPanel{
	
	private CourseController courseController;
	private ArrayList<CourseVO> courseVOs;
	private JTable table;
	private DefaultTableModel model;
	private JButton modifyButton;
	private JScrollPane scrollPane;
	private String courseID;
	private JTextField courseIDField;
	private JTextField courseNameField;
	private JTextField periodField;
	private JTextField courseLocationField;
	private JTextField assistantField;
	private JTextField courseTimeField;
	private JTextArea introduceField;
	private JTextArea referenceBookField;
	private JTextField creditField;
	private JTextField attributeField;
	private String []departmentList=new String[28];
	private String departmentID=null;
	private String teacherName;
	private String teacherID;
	private static Image INFO=new ImageIcon("Graphics/teacherGraphics/info.png").getImage();
	private JTextField textField;
	private JTextField textField_2;
	private int term;
	private MyTabbedPane tabbedPane;
	private String courseName;
	
	public FillCourseInfoPanel(final CourseVO courseVO){
		setSize(800, 600);
		setLayout(null);
		
		term=courseVO.getTerm();
		teacherID=courseVO.getTeacherID();
		teacherName=courseVO.getTeacherName();
		departmentID=courseVO.getDepartmentID();
		courseName=courseVO.getCourseName();
		
		courseController=new CourseController(courseVO.getTeacherID());
		ArrayList<String> departmentArrayList;
		try {
			departmentArrayList = courseController.getAllDepartments();
			for (int i = 0; i<departmentArrayList.size(); i++) {
				departmentList[i]=departmentArrayList.get(i);
			}
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			ExceptionHandler.networkException();
		}
		
		
		
		
		
		
		JLabel lblNewLabel_1 = new JLabel("课程编号");
		
		
		lblNewLabel_1.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		lblNewLabel_1.setBounds(103, 42, 73, 15);
		add(lblNewLabel_1);
		
		JLabel lblNewLabel_2 = new JLabel("课程名称");
		
		lblNewLabel_2.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		lblNewLabel_2.setBounds(389, 42, 75, 15);
		add(lblNewLabel_2);
		
		JLabel lblNewLabel_5 = new JLabel("课程学分");
		lblNewLabel_5.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		lblNewLabel_5.setBounds(389, 94, 75, 15);
		add(lblNewLabel_5);
		
		JLabel lblNewLabel_6 = new JLabel("课       时");
		lblNewLabel_6.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		lblNewLabel_6.setBounds(389, 150, 75, 15);
		add(lblNewLabel_6);
		
		JLabel lblNewLabel_7 = new JLabel("上课地点");
		lblNewLabel_7.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		lblNewLabel_7.setBounds(389, 212, 75, 15);
		add(lblNewLabel_7);
		
		JLabel lblNewLabel_8 = new JLabel("助      教");
		lblNewLabel_8.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		lblNewLabel_8.setBounds(103, 309, 73, 15);
		add(lblNewLabel_8);
		
		JLabel lblNewLabel_9 = new JLabel("上课时间");
		lblNewLabel_9.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		lblNewLabel_9.setBounds(103, 212, 73, 15);
		add(lblNewLabel_9);
		
		JLabel lblNewLabel_10 = new JLabel("课程属性");
		lblNewLabel_10.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		lblNewLabel_10.setBounds(103, 94, 73, 15);
		add(lblNewLabel_10);
		
		JLabel lblNewLabel_11 = new JLabel("院      系");
		lblNewLabel_11.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		lblNewLabel_11.setBounds(103, 150, 73, 15);
		add(lblNewLabel_11);
		
		JLabel label = new JLabel("课程简介");
		label.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		label.setBounds(103, 405, 73, 15);
		add(label);
		
		courseIDField = new JTextField();
		courseIDField.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		
		courseIDField.setText(courseVO.getCourseId());
		courseIDField.setEditable(false);
		courseIDField.setBounds(209, 32, 145, 35);
		add(courseIDField);
		courseIDField.setColumns(10);
		
		courseNameField = new JTextField();
		courseNameField.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		courseNameField.setText(courseVO.getCourseName());
		courseNameField.setEditable(false);
		courseNameField.setBounds(497, 32, 195, 35);
		add(courseNameField);
		courseNameField.setColumns(10);
		
		periodField = new JTextField();
		periodField.setFont(new Font("微软雅黑", Font.PLAIN, 16));

		periodField.setText(String.valueOf(courseVO.getCoursePeriod()));
		

		periodField.setBounds(497, 150, 195, 35);
		add(periodField);
		periodField.setColumns(10);
		
		courseLocationField = new JTextField();
		courseLocationField.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		courseLocationField.setText(courseVO.getClassroom());
		courseLocationField.setBounds(497, 202, 195, 35);
		add(courseLocationField);
		courseLocationField.setColumns(10);
		
		assistantField = new JTextField();
		assistantField.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		assistantField.setText(courseVO.getAssistant());
		System.out.println("助教："+courseVO.getAssistant());
		System.out.println("界面助教："+assistantField.getText());
		assistantField.setBounds(209, 309, 483, 35);
		add(assistantField);
		assistantField.setColumns(10);
		
		courseTimeField = new JTextField();
		courseTimeField.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		courseTimeField.setText(courseVO.getCourseTime());
		System.out.println("时间："+courseVO.getCourseTime());
		
		courseTimeField.setBounds(209, 202, 145, 35);
		add(courseTimeField);
		courseTimeField.setColumns(10);
		
		introduceField = new JTextArea();
		introduceField.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		introduceField.setText(courseVO.getIntroduction());
//		introduceField.setBorder(BorderFactory.createLineBorder(Color.gray,1));
		introduceField.setBounds(209, 399, 483, 75);
		add(introduceField);
		
		JLabel label_1 = new JLabel("参考书目");
		label_1.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		label_1.setBounds(103, 366, 73, 15);
		add(label_1);
		
		referenceBookField = new JTextArea();
		referenceBookField.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		referenceBookField.setText(courseVO.getBook());
//		referenceBookField.setBorder(BorderFactory.createLineBorder(Color.gray,1));
		referenceBookField.setBounds(209, 354, 483, 35);
		add(referenceBookField);
		
		creditField = new JTextField();
		creditField.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		creditField.setText(String.valueOf(courseVO.getCredit()));
		creditField.setEditable(false);
		
		
		creditField.setColumns(10);
		creditField.setBounds(497, 84, 195, 35);
		add(creditField);
		
		attributeField = new JTextField();
		attributeField.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		attributeField.setText(courseVO.getAttribute());
		attributeField.setEditable(false);
		attributeField.setColumns(10);
		attributeField.setBounds(209, 84, 145, 35);
		add(attributeField);
		
		JButton okButton = new JButton("修改");
		okButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				int i=JOptionPane.showConfirmDialog(null,"确认修改课程信息？","确认",JOptionPane.YES_NO_OPTION);
				if(i==JOptionPane.YES_OPTION){
					ResultMessage msg=sendRequest();
					System.out.println("oooooooooooooooo"+msg);
					if(msg==ResultMessage.UPDATE_SUCCESS){
						JOptionPane.showMessageDialog(null,"课程信息添加成功！","提示",JOptionPane.INFORMATION_MESSAGE);
					}else{
						JOptionPane.showMessageDialog(null,"课程信息添加失败,请核对您填写的信息！","提示",JOptionPane.INFORMATION_MESSAGE);
					}
				}
				
			
				
				
				
			}
		});
		okButton.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		okButton.setBounds(262, 484, 130, 34);
		add(okButton);
		
		JButton noButton = new JButton("取消");
		noButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				int i=JOptionPane.showConfirmDialog(null,"取消查看课程信息？","确认",JOptionPane.YES_NO_OPTION);
				if(i==JOptionPane.YES_OPTION){
					System.out.println(tabbedPane.indexOfTab(courseName+" 课程信息")+"KKKKKKKKKK"+courseName);
					tabbedPane.removeOne(tabbedPane.indexOfTab(courseName+" 课程信息"));
					tabbedPane.remove(tabbedPane.indexOfTab(courseName+" 课程信息"));
				}
				
				
			}
		});
		noButton.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		noButton.setBounds(497, 484, 118, 34);
		add(noButton);
		
		JLabel label_2 = new JLabel("最大选课人数");
		label_2.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		label_2.setBounds(103, 263, 96, 22);
		add(label_2);
		
		textField = new JTextField();
		textField.setText(String.valueOf(courseVO.getMaxStudent()));
		textField.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		textField.setColumns(10);
		textField.setBounds(209, 257, 145, 35);
		add(textField);
		
		textField_2 = new JTextField();
		textField_2.setText(courseVO.getDepartmentID());
		textField_2.setEditable(false);
		textField_2.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		textField_2.setColumns(10);
		textField_2.setBounds(209, 148, 145, 35);
		add(textField_2);
		
		

		
		
		
		
	}
	
	public ResultMessage sendRequest() {

	
		if(departmentID==null)
			departmentID="未选择院系";
		
		
		System.out.println(courseIDField.getText()+"%%%%%%%%%%%%%%%%%%%%%%%%"+courseNameField.getText());
		ResultMessage msg=ResultMessage.UPDATE_FAILURE;
		try {
			msg = courseController.updateCourse(new CourseVO(courseIDField.getText(), courseNameField.getText(), teacherID, teacherName, 
					Integer.parseInt(creditField.getText()), Integer.parseInt(periodField.getText()), courseLocationField.getText(), assistantField.getText(), 
					courseTimeField.getText(), attributeField.getText(), Integer.parseInt(textField.getText()),term,departmentID, referenceBookField.getText(), 
					introduceField.getText()));
			
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			ExceptionHandler.networkException();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			ExceptionHandler.networkException();
		}
		
		return msg;
	}
	
	
	
	public void paintComponent(Graphics g){
		g.drawImage(INFO, 0,0, null);
	}


	public void setTabPane(MyTabbedPane tabbedPane) {
		this.tabbedPane=tabbedPane;
		
	}

	

	
}
